You are given a non-negative integer N. Your task is to return the largest exponent e such that 2e divides N! (N factorial).
To find this exponent efficiently, count the number of factors of 2 contributed by all multiples of 2, 4, 8, and higher powers of 2 that do not exceed N. Add these counts to obtain the final result.
Input: N = 10
Output: 8
Explanation: The exponent is calculated as 10 / 2 + 10 / 4 + 10 / 8 = 5 + 2 + 1 = 8.
Input: N = 5
Output: 3
Explanation: The exponent is calculated as 5 / 2 + 5 / 4 = 2 + 1 = 3.
Input: N = 1
Output: 0
Explanation: 1! = 1 is not divisible by 2, so the largest possible exponent is 0.
Approach, complexity and clean code in every language. Sign in to unlock the editorial.
Your submissions, results and runtime history live here. Sign in to start your streak.
5
3
10
8